package top.buukle.buukle.buuk;

import java.util.LinkedList;

public class MovingAverage {

    Integer window;

    Integer sum = 0;

    LinkedList<Integer> list = new LinkedList<>();

    public MovingAverage(Integer window){
        this.window = window;
    }
    public double next(Integer next) {
        list.offer(next);
        sum +=next;
        if(list.size() > window){
            Integer pop = list.pop();
            sum= sum - pop;
        }
        return sum / (double) list.size();
    }

    public static void main(String[] args) {
        MovingAverage movingAverage = new MovingAverage(3);

        System.out.println(movingAverage.next(1));
        System.out.println(movingAverage.next(10));
        System.out.println(movingAverage.next(3));
        System.out.println(movingAverage.next(5));
    }
}
